using System;
using System.Threading.Tasks;
using JlPay.Sdk.Core;
using JlPay.Sdk.Services.Clearing;
using JlPay.Sdk.Models.Clearing;

namespace JlPay.Sdk.Examples.Clearing.WithdrawApply
{
    /// <summary>
    /// 提现申请独立示例程序
    /// </summary>
    public class WithdrawApplyDemo
    {
        /// <summary>
        /// 主程序入口
        /// </summary>
        /// <param name="args">命令行参数</param>
        /// <returns>任务</returns>
        public static async Task Main(string[] args)
        {
            Console.WriteLine("==========================================");
            Console.WriteLine("===== 提现申请独立示例程序 =====");
            Console.WriteLine("==========================================");
            Console.WriteLine();

            await WithdrawApplyDemoAsync();

            Console.WriteLine("==========================================");
            Console.WriteLine("===== 示例运行完毕 =====");
            Console.WriteLine("==========================================");
        }

        /// <summary>
        /// 创建配置对象
        /// </summary>
        /// <returns>配置对象</returns>
        private static Config CreateConfig()
        {
            return DemoConfig.CreateConfigWithCrypto();
        }

        /// <summary>
        /// 构建提现申请请求
        /// </summary>
        /// <returns>请求对象</returns>
        private static WithdrawApplyRequest BuildWithdrawApplyRequest()
        {
            var request = new WithdrawApplyRequest();
            
            // 设置请求参数
            request.OrgNo = "123456";
            request.MerchNo = "849584358120018";  // T0结算商户
            request.Amount = "10";
            request.SettleType = "T0";
            request.OutSettleId = "1234567890128";
            request.NotifyUrl = "https://www.baidu.com";
            request.Postscript = "附言";
            
            return request;
        }

        /// <summary>
        /// 提现申请示例
        /// </summary>
        public static async Task WithdrawApplyDemoAsync()
        {
            Console.WriteLine("开始执行提现申请示例");
            
            try
            {
                // 创建配置对象和清算服务
                var config = CreateConfig();
                var clearingService = new ClearingService(config);
                
                // 创建提现申请请求
                var request = BuildWithdrawApplyRequest();
                
                // 发送请求
                var response = await clearingService.ApplyWithdrawAsync(request);
                
                // 处理响应结果
                response.HandleResponse("提现申请", config);
            }
            catch (Exception ex)
            {
                var config = CreateConfig();
                var response = new WithdrawApplyResponse();
                response.HandleResponseWithException("提现申请", config, ex);
            }
            
            Console.WriteLine("提现申请示例执行完毕");
        }
    }
} 